package com.kobe.algorithm.leetcode.case200;

/**
 * @author ：caopengfei
 * @date ：2021/2/24
 */
public class Solution {
    public int numIslands(char[][] grid) {
        int landsNums = 0;

        if(grid == null || grid.length == 0){
            return 0;
        }
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if(grid[i][j] == '1'){
                    dfs(grid,i,j);
                    landsNums++;
                }
            }
        }
        return landsNums;
    }

    private void  dfs(char [][] grid,int x,int y){
        if(x < 0 || y < 0 || x > grid.length-1 || y > grid[0].length-1 || grid[x][y] == '0'){
            return;
        }
        grid[x][y] = '0';
        dfs(grid,x+1,y);
        dfs(grid,x-1,y);
        dfs(grid,x,y-1);
        dfs(grid,x,y+1);
    }


    public static void main(String[] args) {
        char [][]  grid = {{'1','1','0','1','0'},
                           {'1','1','0','1','0'},
                           {'1','1','0','0','1'},
                           {'0','0','0','1','0'}};

        Solution solution = new Solution();


        System.out.println(solution.numIslands(grid));





    }
}
